package com.module.lemlin.rxhttp

import com.module.lemlin.LoadingDialog
import com.module.lemlin.base.BaseApplication
import com.module.lemlin.mode.SubscribeListener
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.wrapper.annotation.DefaultDomain


@DefaultDomain
const val baseUrl = "http://rz.icarefz.cn"

fun <HttpData : HttpServiceData> subscribe(
    observable: Observable<HttpData>,
    subscribe: SubscribeListener<HttpData>
): Disposable = observable.observeOn(AndroidSchedulers.mainThread())
    .doOnSubscribe {
        subscribe.onStart()
    }.doOnNext {
        if (it.code != 200 && it.code != 201) throw CustomException("${it.code}", it.msg)
    }.doOnError {
        if (it.code == 401) {
            //身份过期，退出登录
            LoadingDialog.show(BaseApplication.INSTANCE)
        }
    }.subscribe({
        subscribe.onSuccess(it)
    }, {
        subscribe.onFailure(it)
    })